package com.laomuji.android.mvvm.http.interceptor

import okhttp3.Request

/**
 * 请求头拦截
 * 这里只处理公共的请求头
 * @author laomuji666
 * @since 2024/2/20
 */
class HeaderInterceptor {
    private val _headerMap: HashMap<String, String> = HashMap()

    fun addRequestHeader(request: Request): Request.Builder {
        val builder: Request.Builder = request.newBuilder()
        for (mutableEntry in _headerMap) {
            builder.removeHeader(mutableEntry.key)
            builder.header(mutableEntry.key, mutableEntry.value)
        }
        return builder
    }

    fun addHeader(key: String, value: String) {
        _headerMap[key] = value
    }

    fun getHeader(key: String): String? {
        return _headerMap[key]
    }
}